﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Comp376A2_9609695
{
    /// <summary>
    /// The Fygar's fire. It is a "monster" to easily handle collisions and destruction
    /// </summary>
    class Fire : Monster, Updateable, Drawable
    {

        public SpriteSheet spritesheet;
        private String animName;
        private List<int> animFrames;
        private int frame;
        private int frameTime;
        private int frameLength;
        private int lifeSpan = 500;
        private int lifeTime = 0;
        private bool stopped = false;

        public String animation
        {
            get { return animName; }
            set
            {
                if (spritesheet.getAnimList().Contains(value))
                {
                    if (animName != value)
                    {
                        animName = value;
                        animFrames = spritesheet.getAnimation(animName);
                        frame = 0;
                        frameTime = 0;
                    }
                }
            }
        }

        public Fire(int startX, int startY, bool shootRight) : base (startX, startY)
        {
            frameLength = 100;
            if (shootRight)
                facing = Direction.Right;
            else
                facing = Direction.Left;
        }

        public override void update(GameTime time, GameController control)
        {
            elapsed = time.ElapsedGameTime.Milliseconds;
            lifeTime += elapsed;
            byte cell = control.level.getCellAt((int)position.X - control.level.X, (int)position.Y - control.level.Y);
            if (cell == 0)
                dead = true;
            if (!dead)
            {
                frameTime += elapsed;
                int flameWidth = 16 * (frame + 1);
                int cellX = 0;
                if (facing == Direction.Left)
                    cellX = (int)(position.X - control.level.X) - flameWidth*2;
                else
                    cellX = (int)(position.X - control.level.X) + flameWidth;
                byte nextCell = control.level.getCellAt(cellX, (int)(position.Y - control.level.Y));
                // Only expand if the next cell isn't completely filled
                if (nextCell != 0 && !stopped)
                {
                    if (frameTime >= frameLength)
                    {
                        frame++;
                        frameTime -= frameLength;
                        if (frame > 2)
                            frame = 2;
                    }
                }
                else
                    stopped = true;
                // If it outlives its lifetime, kill the fire
                if (lifeTime >= lifeSpan)
                    dead = true;
                // If the fire hits the player, kill the player
                if (collides(new Rectangle((int)control.player.position.X + 1, (int)control.player.position.Y + 1, 14, 14)))
                {
                    control.state = GameController.GameState.LoseLife;
                    control.player.state = DigDug.State.Dead;
                }
            }
        }

        public override void reset()
        {
            // Fire is destroyed on reset
            dead = true;
        }

        Texture2D Drawable.getImage()
        {
            return spritesheet.image;
        }

        void Drawable.draw(SpriteBatch renderer)
        {
            // Only draw it if it's not dead
            if (!dead)
            {
                Rectangle sprite = spritesheet.getSprite(animFrames.ElementAt(frame));
                if (facing == Direction.Right)
                {
                    renderer.Draw(spritesheet.image, position, sprite, Color.White, 0f, new Vector2(), 1f, SpriteEffects.FlipHorizontally, 0);
                }
                else
                {
                    renderer.Draw(spritesheet.image, new Vector2(position.X - 16 * (frame + 1), position.Y), sprite, Color.White);
                }
            }
        }

        public override bool collides(Rectangle box, bool pumping = false)
        {
            Rectangle bounds = new Rectangle((int)position.X+1, (int)position.Y+1, (16 * (frame+1))-2, 14);
            if (facing == Direction.Left)
                bounds.X -= 16 * (frame + 1);
            Rectangle intersection = new Rectangle();
            Rectangle.Intersect(ref box, ref bounds, out intersection);
            return (intersection.Width != 0 && intersection.Height != 0);
        }
    }
}
